package bearworker.zxl.service.impl;

import bearworker.zxl.dao.mapper.*;
import bearworker.zxl.dao.mapper.extra.SystemMapper;
import bearworker.zxl.entity.*;
import bearworker.zxl.entity.extra.PrivMenuEntEntity;
import bearworker.zxl.entity.extra.UserMenuEntity;
import bearworker.zxl.service.SystemService;
import bearworker.zxl.utils.CommUtil;
import bearworker.zxl.utils.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 逍遥的熊 on 2017/11/29.
 */

@Service("system")
public class SystemServiceImpl implements SystemService{

    private Logger logger = Logger.getLogger(SystemServiceImpl.class);

    @Autowired
    private ManagerMenuMapper menuMapper;

    @Autowired
    private ManagerEntityMapper entityMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Autowired
    private ManagerPrivilegeMapper privilegeMapper;

    @Autowired
    private ManagerPrivilegeMenuEntiryMapper privilegeMenuEntiryMapper;

    @Autowired
    private ManagerRoleMapper roleMapper;

    Map<String, Object> resMap = new HashMap<>();

    @Override
    public Map<String, Object> addMenu(Map<String, String> params) {

        ManagerMenu menuEntity = new ManagerMenu();
        menuEntity.setMenuName(params.get("menuName"));
        menuEntity.setUrl(params.get("url"));
        menuEntity.setMethod(params.get("method"));
        menuEntity.setParentId(Integer.valueOf(params.get("parentId")));
        menuEntity.setState(1);
        menuEntity.setMenuKey(params.get("menuKey"));
        menuEntity.setIsDisplay(Integer.valueOf(params.get("isDisplay")));
        menuEntity.setSort(Integer.valueOf(params.get("sort")));
        menuEntity.setIsLogin(Integer.valueOf(params.get("isLogin")));
        menuEntity.setCreateUser(Integer.valueOf(params.get("createUser")));
        menuEntity.setUpdUser(Integer.valueOf(params.get("updUser")));

        logger.debug(menuEntity);

        int res = menuMapper.insertSelective(menuEntity);

        if(res > 0){
            resMap = CommUtil.toResMap("0","添加成功",res);
        }else {
            resMap = CommUtil.toResMap("-1","添加失败",res);
        }

        return resMap;

    }

    @Override
    public Map<String, Object> addEntity(Map<String, String> params) {

        ManagerEntity entEntity = new ManagerEntity();

        entEntity.setEntityName(params.get("entityName"));
        entEntity.setUrlId(Integer.valueOf(params.get("urlId")));
        entEntity.setState(1);
        entEntity.setCreateUser(Integer.valueOf(params.get("createUser")));
        entEntity.setUpdUser(Integer.valueOf(params.get("updUser")));

        int res = entityMapper.insertSelective(entEntity);

        if(res > 0){
            resMap = CommUtil.toResMap("0","添加成功",res);
        }else {
            resMap = CommUtil.toResMap("-1","添加失败",res);
        }

        return resMap;
    }

    public Map<String, Object> getMenus(Map<String, String> params){
        List<ManagerMenu> list = menuMapper.selectByParentId(Integer.valueOf(params.get("parentId")));

        resMap = CommUtil.toResMap("0","成功",list);

        return resMap;
    }

    public Map<String,Object> getUserMenus(Map<String, String> params){
        String key = params.get("userId")+"_privi";

        Map<String,String> menus = RedisUtil.get(key);

        resMap = CommUtil.toResMap("0","添加成功",menus.get(key));

        return resMap;
    }



    public Map<String,Object> getAllMenus(Map<String, String> params){
        Map<String,Object> map = new HashMap<>();

        List<ManagerMenu> allMenus = menuMapper.selectMenus(Integer.valueOf(params.get("offset")),Integer.valueOf(params.get("limit")));

        int cnt = menuMapper.selectMenusCnt();

        map.put("menus",allMenus);
        map.put("cnt",cnt);

        resMap = CommUtil.toResMap("0","查询成功",map);

        return resMap;
    }

    public Map<String,Object> delMenus(Map<String, String> params){
        String ids = params.get("ids");
        Integer updUser = Integer.valueOf(params.get("updUser"));

        String[] idAll = ids.split(",");

        for (String id : idAll) {
            ManagerMenu entity = new ManagerMenu();

            entity.setId(Integer.valueOf(id));
            entity.setState(0);
            entity.setUpdUser(updUser);

            int res = menuMapper.updateByPrimaryKeySelective(entity);
        }

        resMap = CommUtil.toResMap("0","删除成功","");

        return resMap;
    }

    public List<UserMenuEntity> getUserPriv(Map<String,Object> paramMap){
        int parentId =(int) paramMap.get("parentId");
        Integer userId = null;
        if(paramMap.containsKey("userId")){
            userId = (int) paramMap.get("userId");
        }
        return systemMapper.getUserPriv(parentId,userId);
    }

    public List<ManagerMenu> getAll(){
        return menuMapper.selectAllMenus();
    }

    public Map<String,Object> getMenu(Map<String, String> params){
        Integer id = Integer.valueOf(params.get("id"));
        ManagerMenu entity = menuMapper.selectByPrimaryKey(id);

        resMap = CommUtil.toResMap("0","获取成功",entity);

        return resMap;
    }

    public Map<String,Object> updMenu(Map<String, String> params){
        ManagerMenu menuEntity = new ManagerMenu();
        menuEntity.setId(Integer.valueOf(params.get("menuId")));
        menuEntity.setMenuName(params.get("menuName"));
        menuEntity.setUrl(params.get("url"));
        menuEntity.setMethod(params.get("method"));
        menuEntity.setParentId(Integer.valueOf(params.get("parentId")));
        menuEntity.setState(1);
        menuEntity.setMenuKey(params.get("menuKey"));
        menuEntity.setIsDisplay(Integer.valueOf(params.get("isDisplay")));
        menuEntity.setSort(Integer.valueOf(params.get("sort")));
        menuEntity.setIsLogin(Integer.valueOf(params.get("isLogin")));
        menuEntity.setUpdUser(Integer.valueOf(params.get("updUser")));

        int res = menuMapper.updateByPrimaryKeySelective(menuEntity);

        resMap = CommUtil.toResMap("0","修改成功",res);

        return resMap;
    }

    public Map<String,Object> getAllPrivi(Map<String, String> params){

        Map<String,Object> map = new HashMap<>();

        int limit = Integer.valueOf(params.get("limit"));
        int offset = Integer.valueOf(params.get("offset"));
        List<ManagerPrivilege> list = privilegeMapper.selectAllPrivilege(offset,limit);

        int cnt = privilegeMapper.selectPrivilegesCnt();

        map.put("privis",list);
        map.put("cnt",cnt);

        resMap = CommUtil.toResMap("0","查询成功",map);

        return resMap;
    }

    public Map<String,Object> getPriviDetail(Map<String, String> params){

        Map<String,Object> map = new HashMap<>();
        List<PrivMenuEntEntity> allPriList = new ArrayList<>();
        Integer id = Integer.valueOf(params.get("id"));
        List<PrivMenuEntEntity> list = systemMapper.getPriviDetail(id);
        List<PrivMenuEntEntity> list2 = systemMapper.getPriviDetail2(id);

        allPriList.addAll(list);
        allPriList.addAll(list2);

        map.put("privid",allPriList);
        map.put("cnt",allPriList.size());

        resMap = CommUtil.toResMap("0","查询成功",map);

        return resMap;
    }

    public Map<String,Object> getPmenus(Map<String, String> params){

        Map<String,Object> map = new HashMap<>();

        Integer priviId = Integer.valueOf(params.get("priviId"));

        List<ManagerMenu> list = systemMapper.getPmenus(priviId);

        List<ManagerMenu> slist = systemMapper.getSmenus(priviId);

        map.put("pmenu",list);
        map.put("smenu",slist);

        resMap = CommUtil.toResMap("0","查询成功",map);

        return resMap;
    }

    @Transactional(value = "tx")
    public Map<String,Object> insPrivi(Map<String, String> params){
        String priviName = params.get("priviName");
        String menuIds = params.get("menuIds");
        Integer userId = Integer.valueOf(params.get("userId"));


        ManagerPrivilege priviEntity = new ManagerPrivilege();
        priviEntity.setPriviName(priviName);
        priviEntity.setCreateUser(userId);
        priviEntity.setUpdUser(userId);
        priviEntity.setState(1);

        Integer priviId = privilegeMapper.insertSelective(priviEntity);

        List<ManagerPrivilegeMenuEntiry> list = new ArrayList<>();
        String[] menuid = menuIds.split(",");
        for(int i = 0;i < menuid.length;i++) {
            ManagerPrivilegeMenuEntiry entEntity = new ManagerPrivilegeMenuEntiry();
            entEntity.setPriviId(priviId);
            entEntity.setMenuId(Integer.valueOf(menuid[i]));
            entEntity.setCreateUser(userId);
            entEntity.setUpdUser(userId);
            entEntity.setState(1);
            privilegeMenuEntiryMapper.insertSelective(entEntity);
        }

        resMap = CommUtil.toResMap("0","插入成功","");

        return resMap;
    }

    @Transactional(value = "tx")
    public Map<String,Object> updPrivi(Map<String, String> params){
        Integer priviId = Integer.valueOf(params.get("priviId"));
        String priviName = params.get("priviName");
        String menuIds = params.get("menuIds");
        Integer userId = Integer.valueOf(params.get("userId"));

        ManagerPrivilege priviEntity = new ManagerPrivilege();
        priviEntity.setPriviName(priviName);
        priviEntity.setId(priviId);
        priviEntity.setUpdUser(userId);

        int ures = privilegeMapper.updateByPrimaryKeySelective(priviEntity);

        int dres = privilegeMenuEntiryMapper.deleteByPriviId(priviId);

        List<ManagerPrivilegeMenuEntiry> list = new ArrayList<>();
        String[] menuid = menuIds.split(",");
        for(int i = 0;i < menuid.length;i++) {
            ManagerPrivilegeMenuEntiry entEntity = new ManagerPrivilegeMenuEntiry();
            entEntity.setPriviId(priviId);
            entEntity.setMenuId(Integer.valueOf(menuid[i]));
            entEntity.setCreateUser(userId);
            entEntity.setUpdUser(userId);
            entEntity.setState(1);
            privilegeMenuEntiryMapper.insertSelective(entEntity);
        }

        resMap = CommUtil.toResMap("0","更新成功","");

        return resMap;
    }

    public Map<String,Object> delPrivi(Map<String, String> params){
        String priviIds = params.get("priviId");
        Integer userId = Integer.valueOf(params.get("userId"));
        String[] ids = priviIds.split(",");
        for(String id : ids) {
            ManagerPrivilege privilege = new ManagerPrivilege();
            privilege.setId(Integer.valueOf(id));
            privilege.setState(0);
            privilege.setUpdUser(userId);

            privilegeMapper.updateByPrimaryKey(privilege);
        }

        resMap = CommUtil.toResMap("0","删除成功","");

        return resMap;
    }


    public Map<String,Object> getAllRole(Map<String, String> params){
        int limit = Integer.valueOf(params.get("limit"));
        int offset = Integer.valueOf(params.get("offset"));

        Map<String,Object> map = new HashMap<>();

        int res = roleMapper.selectRolesCnt();

        List<ManagerRole> list = roleMapper.selectRoles(offset,limit);

        map.put("roles",list);
        map.put("cnt",res);

        resMap = CommUtil.toResMap("0","获取成功",map);

        return resMap;


    }

}
